<template>
  <view>
    <view class="container">
      <view class="submit-order">
        <!-- 收货地址 -->
        <view
          class="delivery-addr "
          @tap="toAddrListPage"
        >
          <view
            v-if="!userAddr"
            class="addr-bg "
          >
            <view class="add-addr">
              <view class="plus-sign-img">
                <image src="@/static/images/icon/plus-sign.png" />
              </view>
              <text>新增收货地址</text>
            </view>
            <view class="arrow empty" />
          </view>
          <view
            v-if="userAddr"
            class="addr-bg whole"
          >
            <view class="addr-icon">
              <image src="@/static/images/icon/addr.png" />
            </view>
            <view class="user-info">
              <text class="item">
                {{ userAddr.consignee }}
              </text>
              <text class="item">
                {{ userAddr.phone }}
              </text>
            </view>
            <view class="addr">
              {{ userAddr.provinceName }}{{ userAddr.cityName }}{{ userAddr.districtName }}{{ userAddr.detail }}
            </view>
            <view class="arrow" />
          </view>
        </view>

        <!-- 商品详情 -->
        <view class="prod-item">

            <view
              class="item-cont"
            >
              <view class="prod-pic">
                <image mode="aspectFit" :src="orderItem.picture" />
              </view>
              <view class="prod-info">
                <view class="prodname">
                  {{ orderItem.title }}
                </view>
              <!--  <view class="prod-info-cont">
                  {{ item.skuName }}
                </view> -->
                <view class="price-nums">
                  <text class="prodprice">
                    <text class="symbol">
                      ￥
                    </text>
                    <text class="big-num">
                      {{ wxs.parsePrice(orderItem.price)[0] }}
                    </text>
                    <text class="small-num">
                      .{{ wxs.parsePrice(orderItem.price)[1] }}
                    </text>
                  </text>
                  <text class="prodcount">
                    x{{ orderItem.price }}
                  </text>
                </view>
              </view>
            </view>


          <view class="total-num">
            <text class="prodcount">
              共 1 件商品
            </text>
            <view class="prodprice">
              小计：
              <text class="symbol">
                ￥
              </text>
              <text class="big-num">
                {{ wxs.parsePrice(orderItem.price)[0] }}
              </text>
              <text class="small-num">
                .{{ wxs.parsePrice(orderItem.price)[1] }}
              </text>
            </view>
          </view>
        </view>

        <!-- 订单详情 -->


        <view class="order-msg">
          <view class="msg-item">

            <!-- <view class="item">
              <view class="item-tit">
                运费：
              </view>
              <view class="item-txt price">
                <text class="symbol">
                  ￥
                </text>
                <text class="big-num">
                  {{ wxs.parsePrice(transfee)[0] }}
                </text>
                <text class="small-num">
                  .{{ wxs.parsePrice(transfee)[1] }}
                </text>
              </view>
            </view> -->
         <!--   <view class="item">
              <view class="item-tit">
                优惠金额：
              </view>
              <view class="item-txt price">
                <text class="symbol">
                  -￥
                </text>
                <text class="big-num">
                  {{ wxs.parsePrice(shopReduce)[0] }}
                </text>
                <text class="small-num">
                  .{{ wxs.parsePrice(shopReduce)[1] }}
                </text>
              </view>
            </view> -->

          </view>
        </view>
      </view>

      <!-- 底部栏 -->
      <view class="submit-order-footer">
        <view class="sub-order">
          <view class="item-txt">
            合计：
            <view class="price">
              <text class="symbol">
                ￥
              </text>
              <text class="big-num">
                {{ wxs.parsePrice(orderItem.price)[0] }}
              </text>
              <text class="small-num">
                .{{ wxs.parsePrice(orderItem.price)[1] }}
              </text>
            </view>
          </view>
        </view>
        <view
          class="footer-box"
          @tap="toPay"
        >
          提交订单
        </view>
      </view>
    </view>

  </view>
</template>

<script setup>
const wxs = number()
let goodsId = '0'
/**
 * 生命周期函数--监听页面加载
 */
onLoad((options) => {
  goodsId = options.goodsId
})

const userAddr = ref('')
/**
 * 生命周期函数--监听页面显示
 */
onShow(() => {
  const pages = getCurrentPages()
  const currPage = pages[pages.length - 1]
  if (currPage.selAddress === 'yes') {
    // 将携带的参数赋值
    userAddr.value = currPage.item
  }
  // 获取订单数据
  loadOrderData()
})

let couponIds = []
const coupons = ref({})
const total = ref(0)
const actualTotal = ref(0)
const orderItem = ref([])
const totalCount = ref(0)
const transfee = ref(0)
const shopReduce = ref('')
/**
 * 加载订单数据
 */
const loadOrderData = () => {
  let addrId = 0
  if (userAddr.value != null) {
    addrId = userAddr.value.addrId
  }
  uni.showLoading({
    mask: true
  })
 //获取默认地址
 http.request({
   url: '/addressBook/defaultAddr',
   method: 'get',
 })
   .then(({ data }) => {
     console.log(data);
     userAddr.value = data
   })
   .catch(err => {
     // chooseCouponErrHandle(err)
   })
 //获取订单商品信息
  http.request({
    url: '/goods/getGoods',
    method: 'get',
    data: {
      id:goodsId
    }
  })
    .then(({ data }) => {
      console.log(data);
      orderItem.value = data

      // actualTotal.value = data.actualTotal
      // total.value = data.total
      // totalCount.value = data.totalCount
      // userAddr.value = data.userAddr
      // transfee.value = data.shopCartOrders[0].transfee
      // shopReduce.value = data.shopCartOrders[0].shopReduce

      uni.hideLoading()
    })
    .catch(err => {
      uni.hideLoading()
      // chooseCouponErrHandle(err)
    })
}

/**
 * 获取默认地址
 */
const loadD=()=>{
  http.request({
    url: '/addressBook/defaultAddr',
    method: 'get',
  })
    .then(({ data }) => {
      console.log(data);
      userAddr.value = data.userAddr
    })
    .catch(err => {
      // chooseCouponErrHandle(err)
    })
}


/**
 * 提交订单
 */
const toPay = () => {
  if (!userAddr.value) {
    uni.showToast({
      title: '请选择地址',
      icon: 'none'
    })
    return
  }
  submitOrder()
}

const submitOrder = () => {
  uni.showLoading({
    mask: true
  })
  http.request({
    url: '/orders/create',
    method: 'POST',
    data: {
        productId: orderItem.value.id,
        // productName:orderItem.value.title,
        // totalPrice: orderItem.value.price,
        addressBookId: userAddr.value.id,
        // sellerId: orderItem.value.userId,
        // sellerName: orderItem.value.userName,
    }
  })
    .then(({ data }) => {
      uni.hideLoading()
      uni.navigateTo({
        url: `/pages/pay/pay?orderId=${data}`
      })
    })
    .catch(res=>{
      uni.hideLoading()
      console.log(res);
    })
}


/**
 * 去地址页面
 */
const toAddrListPage = () => {
  uni.navigateTo({
    url: '/pages/delivery-address/delivery-address?order=0'
  })
}

</script>

<style scoped lang="scss">
@use './submit-order.scss';
</style>
